package com.pur.zsms.matermanage.mater.web;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.accountmanage.vo.AccountVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.filter.AttributeListenerUtil;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.ApplicationUtil;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterValueVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.ShoppingCartMaterVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.systemsetting.datadictionary.service.IDataDictionaryService;

@Controller
@RequestMapping(value = "/materForMobile")
public class MaterControllorForMobile {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(MaterControllorForMobile.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "materService")
	private IMaterService materService;

	@Resource(name = "shopService")
	private IShopService shopService;

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "dictionaryService")
	private IDataDictionaryService dataDictionaryService;

	@RequestMapping(value = "/findMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findMater(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, MaterVo materVo)
			throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = materService.listMater(pageController, materVo);
		List<MaterVo> materList = new ArrayList<MaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				MaterVo f = new MaterVo();
				f.setMaterId((Integer) item[0]);
				f.setMaterName((String) item[1]);
				f.setMaterType((Integer) item[2]);
				f.setMaterLevel((Integer) item[3]);
				f.setMaterValue(((BigDecimal) item[4]).multiply(new BigDecimal("1000")));
				f.setMaterProportion((Integer) item[5]);
				f.setMaterDescribe((String) item[6]);
				f.setSupId((Integer) item[7]);
				f.setSupName((String) item[8]);

				f.setMaterValueUndressed(((BigDecimal) item[9]).multiply(new BigDecimal("1000")));

				f.setUnit((String) item[10]);
				f.setSpecifications((String) item[11]);
				f.setBrands((String) item[12]);
				f.setDeleteFlag((Integer) item[13]);

				materList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", materList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/lookMaterValue.do", method = { RequestMethod.POST })
	@ResponseBody
	public String lookMaterValue(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, MaterVo materVo)
			throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		if (materVo.getInitFlag() == 0) {
			json.put("success", false);
			json.put("total", 0);
			return json.toString();
		}
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = materService.lookMaterValue(pageController, materVo);
		List<MaterVo> materList = new ArrayList<MaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				MaterVo f = new MaterVo();
				f.setMaterId((Integer) item[0]);
				f.setMaterName((String) item[1]);
				f.setMaterType((Integer) item[2]);
				f.setMaterLevel((Integer) item[3]);
				f.setMaterValue(((BigDecimal) item[4]).multiply(new BigDecimal("1000")));
				f.setMaterProportion((Integer) item[5]);
				f.setMaterDescribe((String) item[6]);
				f.setSupId((Integer) item[7]);
				f.setSupName((String) item[8]);
				f.setCustId((Integer) item[10]);

				f.setMaterValueUndressed(((BigDecimal) item[9]).multiply(new BigDecimal("1000")));

				materList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", materList);
		return json.toString();
	}

	@RequestMapping(value = "/lookMaterValueCost.do", method = { RequestMethod.POST })
	@ResponseBody
	public String lookMaterValueCost(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, MaterVo materVo)
			throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		if (materVo.getInitFlag() == 0) {
			json.put("success", false);
			json.put("total", 0);
			return json.toString();
		}
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = materService.lookMaterValueCost(pageController, materVo);
		List<MaterVo> materList = new ArrayList<MaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				MaterVo f = new MaterVo();
				f.setMaterId((Integer) item[0]);
				f.setMaterName((String) item[1]);
				f.setMaterType((Integer) item[2]);
				f.setMaterLevel((Integer) item[3]);
				f.setMaterValue(((BigDecimal) item[4]).multiply(new BigDecimal("1000")));
				f.setMaterProportion((Integer) item[5]);
				f.setMaterDescribe((String) item[6]);
				f.setSupId((Integer) item[7]);
				f.setSupName((String) item[8]);

				f.setMaterValueUndressed(((BigDecimal) item[9]).multiply(new BigDecimal("1000")));

				materList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", materList);
		return json.toString();
	}

	@RequestMapping(value = "/findEditorMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findEditorMater(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, MaterVo materVo)
			throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = materService.listEditorMater(pageController, materVo);
		List<MaterVo> materList = new ArrayList<MaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				MaterVo f = new MaterVo();
				f.setMaterId((Integer) item[0]);
				f.setMaterName((String) item[1]);
				f.setMaterType((Integer) item[2]);
				f.setMaterProportion((Integer) item[3]);

				materList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", materList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/preSaveMater.do", method = { RequestMethod.GET })
	public ModelAndView preSaveMater() {
		/*
		 * JSONObject json = new JSONObject(); List<RoleVo> roleList = null;
		 * 
		 * try { roleList = foodService.listAllRole(); //查询所有角色信息 } catch
		 * (GenericException e) { e.printStackTrace(); }
		 */
		Map map = new HashMap();
		// map.put("rolelist", roleList);
		return new ModelAndView("/matermanage/mater/addMater", map);
	}

	@RequestMapping(value = "/findSelectMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findSelectMater(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, MaterVo materVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		UserPo userInfo = (UserPo) session.getAttribute("user");
		// shopid 去的
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
		materVo.setCustId((int) shopId);
		this.pageList = materService.listSelectMater(pageController, materVo);
		List<MaterVo> materList = new ArrayList<MaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				MaterVo f = new MaterVo();
				f.setMaterId((Integer) item[0]);
				f.setMaterName((String) item[1]);
				f.setMaterType((Integer) item[2]);
				f.setMaterLevel((Integer) item[3]);

				// f.setMaterValue(((BigDecimal) item[4]).multiply(new
				// BigDecimal("1000")));
				f.setMaterProportion((Integer) item[6]);
				f.setMid((Integer) item[7]);
				f.setSupId((Integer) item[8]);
				f.setSupName((String) item[9]);
				// f.setMaterValueUndressed(((BigDecimal) item[10]).multiply(new
				// BigDecimal("1000")));
				f.setUnit((String) item[11]);
				f.setSpecifications((String) item[12]);
				f.setBrands((String) item[13]);
				f.setMaterValueContent("材料价格波动");

				// 根据加价模式得到价钱
				BigDecimal materValueReal = supplierService.getMaterValueRealByMaterIdForLook(f.getMaterId(), f.getSupId(), shopId);
				f.setMaterValueUndressed(materValueReal);
				materList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", materList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/findSelectMaterForUnusualSelf.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findSelectMaterForUnusualSelf(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			MaterVo materVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		UserPo userInfo = (UserPo) session.getAttribute("user");
		materVo.setCustId((int) userInfo.getUserId());
		this.pageList = materService.listSelectMater(pageController, materVo);
		List<MaterVo> materList = new ArrayList<MaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				MaterVo f = new MaterVo();
				f.setMaterId((Integer) item[0]);
				f.setMaterName((String) item[1]);
				f.setMaterType((Integer) item[2]);
				f.setMaterLevel((Integer) item[3]);
				f.setMaterValue(((BigDecimal) item[4]).multiply(new BigDecimal("1000")));
				f.setMaterProportion((Integer) item[6]);
				f.setMid((Integer) item[7]);
				f.setSupId((Integer) item[8]);
				f.setSupName((String) item[9]);
				f.setMaterValueUndressed(((BigDecimal) item[10]).multiply(new BigDecimal("1000")));
				f.setUnit((String) item[11]);
				f.setSpecifications((String) item[12]);
				f.setBrands((String) item[13]);
				f.setMaterValueContent("材料价格波动");
				materList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", materList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/saveMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String saveMater(MaterVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			boolean flag = materService.addMater(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", materService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/pulldownMaterLevel.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownMaterLevel() {
		JSONObject json = new JSONObject();
		String a = "[{\"level\":\"1\",\"levelname\":\"优\"},{\"level\":\"2\",\"levelname\":\"中\"},{\"level\":\"3\",\"levelname\":\"差\"}]";

		return a;
		// return json.toString();
	}

	@RequestMapping(value = "/materTypeFormate.do", method = { RequestMethod.POST })
	public void materTypeFormate(MaterVo materVo, HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		json.put("success", true);
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter writer;
		try {
			String materTypeString = dataDictionaryService.getMaterType();
			writer = response.getWriter();
			writer.write(materTypeString);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (GenericException e) {
			e.printStackTrace();
		}
		// return json.toString();
	}

	@RequestMapping(value = "/pulldownMaterType.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownMaterType(int space) {
		JSONObject json = new JSONObject();
		String jsonString = "";
		try {
			jsonString = materService.getAllMaterTypeJsonString(space);
		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return jsonString;
	}

	@RequestMapping(value = "/preUpdateMater.do", method = { RequestMethod.GET })
	public ModelAndView preUpdateMater(String materId, String supId, String materLevel) {
		// System.out.println(AttributeListenerUtil.getSessions().size());
		JSONObject json = new JSONObject();
		MaterVo materVo = new MaterVo();
		try {
			materVo = materService.getMaterLevelVo(materId, supId, materLevel); // 查询所有角色信息

		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("materVo", materVo);
		return new ModelAndView("/matermanage/mater/updateMater", map);
	}

	@RequestMapping(value = "/updateMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateMater(MaterVo c) {
		JSONObject json = new JSONObject();
		try {
			String rmsg = materService.updateMater(c);
			if (!rmsg.equals("0000")) {
				json.put("success", false);
				json.put("msg", rmsg);
				return json.toString();
			}
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/deleteMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteMater(int materId, int materLevel, int supId) {
		JSONObject json = new JSONObject();
		try {
			materService.deleteMater(materId, materLevel, supId);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/recoverMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String recoverMater(int materId, int materLevel, int supId) {
		JSONObject json = new JSONObject();
		try {
			materService.recoverMater(materId, materLevel, supId);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/backupAnddealOrdValue.do", method = { RequestMethod.POST })
	@ResponseBody
	public String backupAnddealOrdValue() {
		JSONObject json = new JSONObject();
		try {
			// 备份材料
			materService.backupMater();
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	// 导出所有菜品excel
	@RequestMapping(value = "/exportMaterExcel.do", method = { RequestMethod.POST })
	public void exportMaterExcel(HttpServletRequest request, HttpServletResponse response, AccountVo accountVo) {

		HttpSession session = request.getSession();
		session.setAttribute("state", null);
		// 生成提示信息，
		response.setContentType("application/vnd.ms-excel");
		String codedFileName = null;
		OutputStream fOut = null;
		try {
			// 进行转码，使其支持中文文件名
			codedFileName = java.net.URLEncoder.encode("excel", "UTF-8");
			response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");

			HSSFWorkbook workbook = null;

			String path = request.getSession().getServletContext().getRealPath("") + System.getProperty("file.separator") + "jsp"
					+ System.getProperty("file.separator") + "templet" + System.getProperty("file.separator") + "excel.xls";
			InputStream fis = new FileInputStream(path);

			workbook = new HSSFWorkbook(fis);
			// 产生工作簿对象
			// 产生工作表对象
			HSSFSheet sheet = workbook.getSheet("Sheet1");

			// 边框
			HSSFCellStyle setBorder = workbook.createCellStyle();
			setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框

			List<Object[]> listTotal = materService.getAllExcelMater();
			for (int i = 0; i < listTotal.size(); i++) {

				Object[] a = listTotal.get(i);
				HSSFRow row = sheet.createRow((int) i + 1);// 创建一行

				HSSFCell cell1 = row.createCell((int) 0);// 创建一列
				cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell1.setCellValue(a[0].toString());
				cell1.setCellStyle(setBorder);

				HSSFCell cell2 = row.createCell((int) 1);// 创建一列
				cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell2.setCellValue(a[1].toString());
				cell2.setCellStyle(setBorder);

				HSSFCell cell3 = row.createCell((int) 2);// 创建一列
				cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell3.setCellValue(a[2].toString());
				cell3.setCellStyle(setBorder);

				HSSFCell cell4 = row.createCell((int) 3);// 创建一列
				cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell4.setCellValue(a[3].toString());
				cell4.setCellStyle(setBorder);

				HSSFCell cell5 = row.createCell((int) 4);// 创建一列
				cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell5.setCellValue(a[4].toString());
				cell5.setCellStyle(setBorder);

				HSSFCell cell6 = row.createCell((int) 5);// 创建一列
				cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell6.setCellValue(a[5].toString());
				cell6.setCellStyle(setBorder);

				HSSFCell cell7 = row.createCell((int) 6);// 创建一列
				cell7.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell7.setCellValue(a[6].toString());
				cell7.setCellStyle(setBorder);

			}

			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
		} catch (Exception e) {
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
			}
			session.setAttribute("state", "open");
		}
	}

	// 导出
	@RequestMapping(value = "/exportMaterCustExcel.do", method = { RequestMethod.POST })
	public void exportMaterCustExcel(HttpServletRequest request, HttpServletResponse response, HttpSession session1, AccountVo accountVo) {

		HttpSession session = request.getSession();
		UserPo userInfo = (UserPo) session1.getAttribute("user");
		session.setAttribute("state", null);
		// 生成提示信息，
		response.setContentType("application/vnd.excel");
		String codedFileName = null;
		OutputStream fOut = null;
		try {
			// 进行转码，使其支持中文文件名
			codedFileName = java.net.URLEncoder.encode("excelCust", "UTF-8");
			response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");

			HSSFWorkbook workbook = null;

			String path = request.getSession().getServletContext().getRealPath("") + System.getProperty("file.separator") + "jsp"
					+ System.getProperty("file.separator") + "templet" + System.getProperty("file.separator") + "excelcust.xls";
			InputStream fis = new FileInputStream(path);

			workbook = new HSSFWorkbook(fis);
			// 产生工作簿对象
			// 产生工作表对象
			HSSFSheet sheet = workbook.getSheet("Sheet1");
			sheet.setColumnWidth(1, 5000);

			// 边框
			HSSFCellStyle setBorder = workbook.createCellStyle();
			setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框

			List<Object[]> listTotal = materService.getAllExcelMaterCust(userInfo.getUserId());
			log.info("zzy001--------------8");

			for (int i = 0; i < listTotal.size(); i++) {
				log.info("zzy001--------------8:" + i + "--------1");

				Object[] a = listTotal.get(i);
				HSSFRow row = sheet.createRow((int) i + 1);// 创建一行

				HSSFCell cell1 = row.createCell((int) 0);// 创建一列
				cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell1.setCellValue(a[0].toString());
				cell1.setCellStyle(setBorder);

				HSSFCell cell2 = row.createCell((int) 1);// 创建一列
				cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell2.setCellValue(a[1].toString());
				cell2.setCellStyle(setBorder);

			}

			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
		} catch (Exception e) {
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
			}
			session.setAttribute("state", "open");
		}
	}

	@RequestMapping(value = "/preLookMaterValue.do", method = { RequestMethod.GET })
	public ModelAndView preLookMaterValue(MaterValueVo materValueVo) {
		JSONObject json = new JSONObject();
		try {
			materValueVo = materService.getMaterValueVo(materValueVo);

		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("materValueVo", materValueVo);
		return new ModelAndView("/matermanage/look/lookMaterDetailed", map);
	}

	@RequestMapping(value = "/preLookMaterValueCost.do", method = { RequestMethod.GET })
	public ModelAndView preLookMaterValueCost(MaterValueVo materValueVo) {
		JSONObject json = new JSONObject();
		try {
			materValueVo = materService.getMaterValueVoCost(materValueVo);

		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("materValueVo", materValueVo);
		return new ModelAndView("/matermanage/look/lookMaterDetailed", map);
	}

	@RequestMapping(value = "/converterSpell.do", method = { RequestMethod.POST })
	@ResponseBody
	public String converterSpell() {
		JSONObject json = new JSONObject();
		try {
			materService.converterSpell();

		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		json.put("success", true);
		return json.toString();
	}

	@RequestMapping(value = "/addShoppingCart.do", method = { RequestMethod.POST })
	@ResponseBody
	public String addShoppingCart(ShoppingCartMaterVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			c.setCustId((int) shopId);

			boolean flag = materService.addShoppingCart(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", materService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/findShoppingCartMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findShoppingCartMater(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			ShoppingCartMaterVo shoppingCartMaterVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		page = "1";
		rows = "1000";

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
		shoppingCartMaterVo.setCustId((int) shopId);
		this.pageList = materService.findShoppingCartMater(pageController, shoppingCartMaterVo);
		List<ShoppingCartMaterVo> shoppingCartMaterVoList = new ArrayList<ShoppingCartMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				ShoppingCartMaterVo f = new ShoppingCartMaterVo();
				f.setSid((Integer) item[0]);
				f.setMaterId((Integer) item[1]);
				f.setMaterLevel((Integer) item[2]);
				f.setMaterValue(((BigDecimal) item[3]).multiply(new BigDecimal(1000)));
				f.setMaterProportion((Integer) item[4]);
				f.setMaterDescribe((String) item[5]);
				f.setMaterValueUndressed(((BigDecimal) item[6]).multiply(new BigDecimal(1000)));
				f.setSupId((Integer) item[7]);
				f.setMaterNum((BigDecimal) item[8]);
				f.setMaterName((String) item[9]);
				f.setUnit((String) item[10]);
				f.setSpecifications((String) item[11]);
				f.setBrands((String) item[12]);
				f.setSupName((String) item[13]);
				f.setLeaveMessage((String) item[14]);
				shoppingCartMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", shoppingCartMaterVoList);
		return json.toString();
	}

	@RequestMapping(value = "/deleteShoppingCartOne.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteShoppingCartOne(ShoppingCartMaterVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			boolean flag = materService.deleteShoppingCartOne(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", materService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/getMaterPo.do", method = { RequestMethod.POST })
	@ResponseBody
	public String getMaterPo(HttpSession session, HttpServletRequest request, HttpServletResponse response, int materId) {
		JSONObject json = new JSONObject();
		boolean flag = true;
		try {
			MaterPo materPo = materService.getMaterPo(materId);
			json.put("materPo", materPo);

		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
			return json.toString();
		}
		json.put("success", true);
		return json.toString();
	}

	@RequestMapping(value = "/copyMaterForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String copyMaterForSup(int materId, int materLevel, int supId, int supIdCopy) {
		JSONObject json = new JSONObject();
		try {
			MaterLevelPo tMaterLevelPo = materService.copyMaterForSup(materId, materLevel, supId, supIdCopy);
			if (tMaterLevelPo != null) {

				materService.addMaterLevel(tMaterLevelPo);
				json.put("success", true);
			} else {
				json.put("msg", materService.getMsg());
				json.put("success", false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

}
